***This do-file runs the regressions found in table O4 of the appendix
**Curtis RESTAT
**Change to directory where data and code were downloaded
**cd 

use nei_raw_tableO4.dta,clear
	gen naics3=substr(naics,1,3)
	tostring fips,gen(fipsstr)
	replace fipsstr="0"+fipsstr if fips<10000
	gen fipst=substr(fipsstr,1,2)
	destring fipst,gen(fipsnum)
	keep if inlist(fipsnum,1,9,10,11,17,18,21,24,25,26,34,36,37,39,42,44,45,47,51,54)
	drop fips
	gen fips=fipsstr
	so fips
	merge m:1 fips using Mich_AL_ctys_tableO4
	drop if nbp04==0
	collapse (sum) nox,by(naics3)
	so naics3
	rename nox nox_all
save naics3_allnbp_tableO4,replace





****Creates number of NBP plants in each industry 

use nei_raw_tableO4.dta,clear
*use "/home/spec237/Desktop/NBPdata/emissionsdata/nei_raw2016.dta",clear
	keep if ampd_id~=""
	gen naics3=substr(naics,1,3)
	gen naics1=substr(naics,1,1)
	keep if naics1=="3"
	replace naics3="333" if naics3=="33"
	tostring fips,replace
	gen cntyind=st+fips+naics3
	unique NEI_Site_ID
	unique cntyind
	unique Facility_Site
	*unique Facility_Site,by(naics3) gen(plantno) 
	duplicates drop Facility_Site,force
	gen plantno=1
	collapse (sum) plantno,by(naics3)
	so naics3
save plantno_nei_tableO4,replace

****Creates number of NEI plants in each industry 
use nei_raw_tableO4.dta,clear
	gen naics3=substr(naics,1,3)
	gen naics1=substr(naics,1,1)
	keep if naics1=="3"
	replace naics3="333" if naics3=="33"
	unique NEI_Site_ID
	unique Facility_Site
	tostring fips,gen(fipsstr)
	replace fipsstr="0"+fipsstr if fips<10000
	gen fipst=substr(fipsstr,1,2)
	destring fipst,gen(fipsnum)
	keep if inlist(fipsnum,1,9,10,11,17,18,21,24,25,26,34,36,37,39,42,44,45,47,51,54)
	drop fips
	gen fips=fipsstr
	so fips
	merge m:1 fips using Mich_AL_ctys_tableO4
	drop if nbp04==0
	duplicates drop Facility_Site,force
	gen plantnoall=1
	collapse (sum) plantnoall,by(naics3)
	so naics3
	drop if inlist(naics3,"31","31-","32")
save plantno_neiall_tableO4,replace

***Generates NOx Intensity Var
use nei_raw_tableO4.dta,clear
	gen naics3=substr(naics,1,3)
	gen naics1=substr(naics3,1,1)
	keep if naics1=="3"
	collapse (sum) nox,by(naics3)
	merge 1:1 naics3 using vship2005_tableO4
	gen nox_int=nox/vship2005
	drop _merge
	so naics3
save nox_int_tableO4,replace

***Generates total emissions that are regulated by NBP by industry
use nei_raw_tableO4.dta,clear
	keep if ampd_id~=""
	gen naics3=substr(naics,1,3)
	replace naics3="333" if naics3=="33"
	drop if st=="MO"
	collapse (sum) nox (first) naics_des,by(naics3)
	rename nox nox_regnbp

***Merges regulated ind data to all ind data
so naics3
merge 1:1 naics3 using naics3_allnbp_tableO4
	gen reg_per=nox_reg/nox_all
	gen naics1=substr(naics3,1,1)
	keep if naics1=="3"
	drop if inlist(naics3,"31","32","33","31-")
	replace nox_reg=0 if nox_reg==.
	replace reg_per=0 if reg_per==.
	drop _merge
	so naics3
	merge 1:1 naics3 using cbp_ind_emp_tableO4
	gen noxemp=nox_all/emp
	gen noxregemp=nox_reg/emp
	drop _m
	so naics3
	merge 1:1 naics3 using plantno_nei_tableO4
	replace plantno=0 if plantno==.
	drop _m
	so naics3
	merge 1:1 naics3 using plantno_neiall_tableO4
	replace plantno=0 if plantno==.
	drop _m naics1
	so naics3
	merge 1:1 naics3 using enint1998_tableO4
	so naics3
	drop _m
	merge 1:1 naics3 using nox_int_tableO4
	drop _m
	order naics3 naics_des plantno plantnoall nox_regnbp nox_all reg_per nox_int enint
	drop if inlist(naics3,"31","32","33","31-","")
	outsheet using "nei_table_tableO4.csv",comma replace
	br


